<template>
  <div>
    <el-form :model="myForm" :rules="myRules" ref="qwe" label-width="100px">
      <el-form-item label="图书名称" prop="name">
        <el-input v-model="myForm.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('qwe')">立即添加</el-button>
        <el-button @click="resetForm('qwe')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="bName"
        label="书名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="bAuth"
        label="作者"
        width="180">
      </el-table-column>
      <el-table-column
        prop="bPrice"
        label="价格">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'BookList',
  data () {
    return {
      myForm: {
        name: ''
      },
      myRules: {
        name: [
          { required: true, message: '请输入图书名称', trigger: 'blur' }
        ]
      },
      tableData: [
        {
          bName: '红楼梦',
          bAuth: '曹雪芹',
          bPrice: '66'
        }, {
          bName: '西游记',
          bAuth: '吴承恩',
          bPrice: '33'
        }, {
          bName: '三国演义',
          bAuth: '罗贯中',
          bPrice: '22'
        }, {
          bName: '水浒传',
          bAuth: '施耐庵',
          bPrice: '33'
        }]
    }
  },
  methods: {
    submitForm (qwe) {
      this.$refs[qwe].validate((valid) => {
        if (valid) {
          this.tableData.push({bName: this.myForm.name,bAuth: 'zd',bPrice: '99'})
          alert('添加成功!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (qwe) {
      this.$refs[qwe].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
